
package cnhy.salary.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cnhy.salary.domain.Employee;
import cnhy.salary.domain.dto.EmployeeDto;
import cnhy.salary.service.EmployeeSvc;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import lombok.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import cnhy.salary.domain.OvertimeRecord;
//import cnhy.salary.service.OvertimeRecordService;
import ling.common.*;
import ling.common.domain.*;

 /** 加班记录 控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2025-5-26
 */
@Api(tags = "加班记录 功能接口")
@RestController
@RequestMapping("/salary/OvertimeRecord")
public class OvertimeRecordController extends EQueryController<OvertimeRecord>{
    

    
    @ApiOperation("详情")
    @PreAuthorize("@ps.any(@pay.overtimeRecord.query)")
    @GetMapping(value = "/{id}")
    public Result<OvertimeRecord> getInfo(@PathVariable("id") Long id)
    {
       return super.getInfo(id);
    }


     @Resource
     EmployeeSvc employeeSvc;
    @ApiOperation("分页列表")
    @PreAuthorize("@ps.any(@pay.overtimeRecord.list)")
    @GetMapping("list")
    public ResultRows<OvertimeRecord> list(OvertimeRecord dto, PageDomain pageRequest) {

        var rows=queryPage(dto,pageRequest);
        employeeSvc.employeeDtoFindSet(rows.getData(),p->p.getUserId(),(p,p1)->{
            p.ExtendMapSet("employeeDto",p1);
        });
        return rows;
    }
    
    @ApiOperation("新增")
    @PreAuthorize("@ps.any(@pay.overtimeRecord.add)")
    @Log(title = "加班记录", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated @RequestBody OvertimeRecord entity) {
        var emplDto = queryHelper.queryable(EmployeeDto.class).where(p -> p.eq(Employee::getUserId, entity.getUserId())).firstOrNull();
        entity.setLv2Id(emplDto.getDeptId());
        entity.setLv2Name(emplDto.getDeptName());
        var m=DateUtil.between(entity.getBeginTime(),entity.getEndTime(), DateUnit.MINUTE);
        entity.setDuration((double)m);
        return super.add(entity);
    }
     @ApiOperation("批量新增")
     @PreAuthorize("@ps.any(@pay.overtimeRecord.add)")
     @Log(title = "加班记录", businessType = BusinessType.INSERT)
     @PostMapping("adds")
     public Result adds(@Validated @RequestBody OvertimeRecord[] entitys) {
        var list=new ArrayList<OvertimeRecord>();
        for (var entity:entitys) {
            var emplDto = queryHelper.queryable(EmployeeDto.class).where(p -> p.eq(Employee::getUserId, entity.getUserId())).firstOrNull();
            entity.setLv2Id(emplDto.getDeptId());
            entity.setLv2Name(emplDto.getDeptName());
            var m = DateUtil.between(entity.getBeginTime(), entity.getEndTime(), DateUnit.MINUTE);
            entity.setDuration((double) m);
           list.add(entity);
        }
         return  super.add(list);
     }
    
    @ApiOperation("删除")
    @PreAuthorize("@ps.any(@pay.overtimeRecord.remove)")
    @Log(title = "加班记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable("ids") Long[] ids){
        return super.remove(Arrays.stream(ids).toArray());
    }
    
    @ApiOperation("编辑")
    @PreAuthorize("@ps.any(@pay.overtimeRecord.edit)")
    @Log(title = "加班记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public  Result edit(@Validated @RequestBody OvertimeRecord entity){
        return super.edit(entity);
    }
    
    @ApiOperation("导出")
    @PreAuthorize("@ps.any(@pay.overtimeRecord.export)")
    @Log(title = "加班记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OvertimeRecord entity, PageDomain pageRequest,HttpServletResponse response)
    {
        super.export(entity,pageRequest,null,response);
    }
}